<?php
include('modules/connection.php');

function checkUser($username, $pass) {
    $conn = connectDB();

    $stm = "SELECT username FROM admin WHERE (username = :username) AND (password = :pass)";
    $prepared = $conn->prepare($stm);
    $prepared->bindParam(':username', $username);
    $prepared->bindParam(':pass', $pass);
    $prepared->execute();
    if ($prepared->rowCount() != 0)
        return 1;
    else {
        $stm = "SELECT username FROM customer WHERE (username = :username) AND (password = :pass)";
        $prepared = $conn->prepare($stm);
        $prepared->bindParam(':username', $username);
        $prepared->bindParam(':pass', $pass);
        $prepared->execute();
        if ($prepared->rowCount() != 0)
            return 2;
    }
    return 0;

    closeDB($conn);
}

function set_login() {
    $username = $_POST['username'];
    $pass = $_POST['pass'];
    $k = checkUser($username, $pass);
    if ($k == 0) return false;
    if ($k == 1) $_SESSION['admin'] = $username;
    else
        if ($k == 2) $_SESSION['user'] = $username;
    return true;
}

?>
